package com.base.controller.dsky;

import com.base.po.param.NetState;
import com.base.po.voip.SbdDetail;
import com.base.po.voip.Subscribers;
import com.base.service.SbdDetailService;
import com.base.service.SubscribersService;
import com.base.utils.DateUtil;
import com.base.utils.MapUtils;
import com.beust.jcommander.internal.Lists;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;
import java.util.Map;

@Api(tags = "SBD金额相关接口")
@RestController
@Slf4j
@RequestMapping("/sbdDetail")
public class SbdDetailController {
    private final SbdDetailService sbdDetailService;
    private final SubscribersService subscribersService;

    public SbdDetailController(SbdDetailService sbdDetailService, SubscribersService subscribersService) {
        this.sbdDetailService = sbdDetailService;
        this.subscribersService = subscribersService;
    }


    @ApiOperation("查询个人的SBD金额信息,statement传入100即为查询所有")
    @PostMapping("/getUserCount")
    public NetState getUserCount(@ApiParam(value = "用户ID") @RequestParam String userId, @ApiParam(value = "状态:1.自购反 2.分销返利 3.转账收益 100.查询全部") @RequestParam Integer statement) {
        BigDecimal money = sbdDetailService.getSbdDetailMoney(userId, statement).setScale(2, RoundingMode.HALF_DOWN);
        return new NetState(NetState.SUCCESS_CONN, "查询成功", money);
    }

    @ApiOperation("获取SBD金额详细信息,statement传入100即为查询所有")
    @PostMapping("/getUserSbdMoneyList")
    public NetState getUserSbdMoneyList(@ApiParam(value = "用户ID") @RequestParam String userId, @ApiParam(value = "状态:1.自购反 2.分销返利 3.转账收益 100.查询全部") @RequestParam Integer statement, @ApiParam(value = "页码 从1开始") @RequestParam Integer pageNo) {
        if (pageNo < 1) pageNo = 1;
        List<SbdDetail> detailList = sbdDetailService.getDetailMoneyList(userId, statement, pageNo);
        return new NetState(NetState.SUCCESS_CONN, "查询成功", detailList);
    }

    @ApiOperation("web用搜索")
    @PostMapping("/search")
    public NetState search(@ApiParam(value = "mobile") @RequestParam(required = false) String mobile, @ApiParam(value = "页码") @RequestParam Integer pageNo) {
        String subId = null;
        if (!StringUtils.isEmpty(mobile)) {
            Subscribers subscribers = subscribersService.selectBySubscribers(mobile, null);
            subId = subscribers.getId();
        }
        List<SbdDetail> sbdDetails = sbdDetailService.getDetailMoneyList(subId, 100, pageNo);
        Integer count=sbdDetailService.getDetailMoneyCount(subId, 100);
        List<Map<String,Object>> list= Lists.newArrayList();
        sbdDetails.forEach(item->{
            Map<String,Object> map= MapUtils.object2Map(item);
            Subscribers subscribers=subscribersService.selectByPrimaryKey(item.getSbdUser());
            if (subscribers!=null){
                map.put("mobile",subscribers.getMobile());
            }
            map.put("time", DateUtil.dateToStrLong(item.getSbdTime()));
            list.add(map);
        });
        return new NetState(NetState.SUCCESS_CONN, "查询成功", list);
    }

    @ApiOperation("删除专用接口")
    @PostMapping("/del")
    public NetState del(@ApiParam(value = "id") @RequestParam Integer id) {
        sbdDetailService.deleteByPrimaryKey(id);
        return new NetState(NetState.SUCCESS_CONN, "删除成功", null);
    }

    @ApiOperation("更新专用接口")
    @PostMapping("/update")
    public NetState update(@ApiParam(value = "id") @RequestParam Integer id, @ApiParam(value = "money") @RequestParam BigDecimal money, @ApiParam(value = "1加0减") @RequestParam Integer status) {
        SbdDetail sbdDetail = sbdDetailService.selectByPrimaryKey(id);
        if (sbdDetail == null) return new NetState(NetState.FEAILED, "查询不到该记录");
        sbdDetail.setSbdTradeMoney(money);
        sbdDetail.setSbdStatus(status);
        sbdDetailService.updateByPrimaryKeySelective(sbdDetail);
        return new NetState(NetState.SUCCESS_CONN, "修改成功", sbdDetail);
    }
}
